package com.leetcode.studyplan.algorithm.introduction.thirdday;

public class TwoSum_II_InputArrayIsSorted {

    /**
     *  双指针，二分查找
     *  第一个指针，指向第一个元素，第二指针完成二分查找
     * @param numbers
     * @param target
     * @return
     */
    public int[] twoSum(int[] numbers, int target) {
        int low = 0;
        int high = numbers.length - 1;

        while (low < high) {
            int result = numbers[low] + numbers[high];
            if (result > target) {
                high--;
            } else if (result < target) {
                low++;
            }
            if (result == target) {
                return new int[] {low + 1, high + 1};
            }
        }

        return null;
    }
}
